package common.util.http;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;

import javax.net.ssl.SSLException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;

/**
 * Author: dong peng
 * Date: 2018/2/2
 * Time: 17:09
 */


public class HttpClientInterface {


    /**
     * 个性化定制属性，  这里设置keeplive的时间
     *
     * @throws IOException
     */
    public void createKeepLive() throws IOException {
        ConnectionKeepAliveStrategy keeepAliveStrategy = new DefaultConnectionKeepAliveStrategy() {

            @Override
            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                long keepLive = super.getKeepAliveDuration(response, context);

                System.out.println("++" + keepLive);
                if(keepLive == -1) {
                    keepLive = 5000;
                }

                return keepLive;
            }
        };

        // HttpClient的实例是线程安全的
        CloseableHttpClient httpClient = HttpClients.custom()
                .setKeepAliveStrategy(keeepAliveStrategy)
                .build();

        httpClient.execute(new HttpGet("http://ifeve.com/httpclient-1-1/"));

    }

    public void  retryStrategy() {
        HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
            public boolean retryRequest(
                    IOException exception,
                    int executionCount,
                    HttpContext context) {
                if (executionCount >= 5) {
// Do not retry if over max retry count
                    return false;
                }
                if (exception instanceof InterruptedIOException) {
// Timeout
                    return false;
                }
                if (exception instanceof UnknownHostException) {
// Unknown host
                    return false;
                }
                if (exception instanceof ConnectTimeoutException) {
// Connection refused
                    return false;
                }
                if (exception instanceof SSLException) {
// SSL handshake exception
                    return false;
                }
                HttpClientContext clientContext = HttpClientContext.adapt(context);
                HttpRequest request = clientContext.getRequest();
                boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
                if (idempotent) {
// Retry if the request is considered idempotent
                    return true;
                }
                return false;
            }
        };
    }

    public static void main(String[] args) throws IOException {
        HttpClientInterface hi = new HttpClientInterface();
        hi.createKeepLive();
    }
}
